<?php
/**
 * Simple XML Parser
 *
 * This is a simple class to provide a simple parser to manage XML documents.
 * The main feature of this is to provide simple access to simple XML documents,
 * translate the XML-tree to Array-based object and reverse.  For more
 * information about this class, view the documentation available in:
 * http://code.apperture.org/multi-purpose/core/mime-types/XML
 *
 * @category  mime-types
 * @package   CORE-xml
 * @author    Juan Manuel Lemus <juan.lemus@apperture.org>
 * @copyright (c) 2008 by Apperture, Inc 
 * @license   http://www.apperture.org/es/licenses/apperture/gnugpl-3.0
 * @version   CVS: $Id: xml.php,v 0.009 2008/03/07 10:57:00 cellog Exp $
 * @link      http://download.apperture.org/multi-purpose/core/mime-types.tar.gz
 **/

/**
 * Loading Dependencies
 **/
poc_require("core/array.php");

class xml
{
    // {{{ properties
     
    /**
     * Return as and Supported MIME-type
     *
     * @var    string : array OR json
     * @access public 
     **/
    var $_as = "array";
     
    /**
     * XML document path
     *
     * @var    string
     * @access public
     **/
    var $xmlDocument = "";
     
    /**
     * XML temp Array
     *
     * @var    array
     * @access private
     **/
    private $xmlArray;
     
    /**
     * Current Parsed-tag
     *
     * @var    string
     * @access private
     **/
    private $xml_current_tag;
     
    /**
     * XML Object
     *
     * @var    object
     * @access private
     **/
    private $xmlHwnd;
         
    // }}}
     
    // {{{ public functions
     
    /**
     * Open a XML-file and create xml-triggers
     *
     * @param  string : xml accessible file
     * @access public
     * @return object : array OR json
     **/
    function open( $xmlDocument, $_as = NULL )
    {
        # Setting-up configuration functions
        $this->xmlHwnd = xml_parser_create();
        xml_set_object         ($this->xmlHwnd, &$this); 
        xml_set_element_handler($this->xmlHwnd, "_xml_start_element", "_xml_end_element" );
        
        # Getting File content
        if ( ($xml_document_content = @file_get_contents( $xmlDocument ) ) == false )
            return false;
        
        $this->xmlDocument = $xmlDocument;
        
        # Executing Parser
        xml_parse($this->xmlHwnd, $xml_document_content);
        
        print_r($this->xmlArray);
    }
     
    /**
     * Fix the Array Entry
     *
     * @param  array  
     * @param  string : array keys refference
     * @return string
     * @access private
     **/
    private function _fix_array_keys( $object, $key )
    {                                                                                                                                        
        $keys  = split('"]', str_replace('["', '', $key) );
        $_key  = "";
            
        for ($i = 1; $i < count($keys) - 1; $i++) {
            $_key    .= '["' . $keys[$i] . '"]';
            $keycount = 0;
            
            $keyindex = array_keys_list( $object, '["%key%"]' );
            $keycount = 0;
            
            foreach ($keyindex as $index)
                if ( strpos($index, $_key) !== false) $keycount++;
                
            if ($keycount > 0) 
                $_key .= '["' . $keycount . '"]';
        }
        
        return $key;
    }
     
    /**
     * Start Elements-evaluation function
     *
     * @param  object : xml parsers
     * @param  string
     * @param  array
     * @access private
     **/
    private function _xml_start_element( $xmlparser, $tagname, $attribs )
    {
        $this->xml_current_tag .= '["' . $tagname . '"]';
       
        $keyindex = $this->_fix_array_keys( $this->xmlArray, $this->xml_current_tag);
        
        if ($attribs != NULL)
            eval('$this->xmlArray' . $keyindex . '["-ATTRIBS"] = $attribs;');
    }
     
    /**
     * End Elements-evaluation function
     * 
     **/
    private function _xml_end_element( $xmlparser, $tagname )
    {
        $this->xml_current_tag = str_replace('["' . $tagname . '"]', "", $this->xml_current_tag);
    }
     // }}}
}
?>